package com.ycsj.goldmedalnewconcept.fragment;


import io.rong.imkit.RongIM;

import com.ycsj.goldmedalnewconcept.ButtonActivity;
import com.ycsj.goldmedalnewconcept.MainActivity;
import com.ycsj.goldmedalnewconcept.StartActivity;
import com.ycsj.goldmedalnewconcept.activity.AboutActivity;
import com.ycsj.goldmedalnewconcept.activity.AppdownloadActivity;
import com.ycsj.goldmedalnewconcept.activity.BirthdayListActivity;
import com.ycsj.goldmedalnewconcept.activity.ClassListActivity;
import com.ycsj.goldmedalnewconcept.activity.CommentActivity;
import com.ycsj.goldmedalnewconcept.activity.GuanwangActivity;
import com.ycsj.goldmedalnewconcept.activity.LeaveActivity;
import com.ycsj.goldmedalnewconcept.activity.LeaveParentListActivity;
import com.ycsj.goldmedalnewconcept.activity.LoginActivity;
import com.ycsj.goldmedalnewconcept.activity.MyRedListActivity;
import com.ycsj.goldmedalnewconcept.activity.NewVersionActivity;
import com.ycsj.goldmedalnewconcept.activity.ParentsEvaluationActivity;
import com.ycsj.goldmedalnewconcept.activity.RedPackageRecordActivity;
import com.ycsj.goldmedalnewconcept.activity.RedVerifyActivity;
import com.ycsj.goldmedalnewconcept.activity.SettingActivity;
import com.ycsj.goldmedalnewconcept.activity.TeacherShowActivity;
import com.ycsj.goldmedalnewconcept.activity.TipsActivity;
import com.ycsj.goldmedalnewconcept.activity.UserInfoActivity;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;
import com.ycsj.goldmedalnewconcept.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView.FindListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

//助手
public class HelperFragment extends Fragment implements OnClickListener{

	private Activity ctx;                                                                    
	private View layout;
	private MainActivity parentActivity;
	private LinearLayout ll_userInfo;    		//个人资料
	private LinearLayout ll_notify;		 		//开课通知
	private LinearLayout ll_birthday;			//生日提醒
	private LinearLayout ll_createHuodong;  	//发布活动
	private LinearLayout ll_appdownload1;		//应用下载上
	private LinearLayout ll_comment;			//评价老师
	private LinearLayout ll_leave;				//在线请假
	private LinearLayout ll_tips;				//备忘录
	private LinearLayout ll_tips2;				//备忘录上
	private LinearLayout ll_redpacket;			//红包管理上
	private LinearLayout ll_appdownload2;		//应用下载下
	private LinearLayout ll_setting;			//设置
	private LinearLayout ll_qiehuanApp;			//切换APP
	private LinearLayout ll_guanwang;			//官网
	private LinearLayout ll_kefu;				//客服
	private LinearLayout ll_about;				//关于
	private LinearLayout ll_banbenjieshao;		//版本介绍
	private LinearLayout ll_jiaoshifengcai;		//教师风采
	private LinearLayout ll_redpacket2;			//红包下
	private TextView tv_exit;					//登出
	private static final String SHARE_PREFS_NAME = "config";
	private String role = "";
	private Intent intent;
	private LinearLayout ll_bg;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		role = SPUtil.getString(getActivity(), "role", "");// 获取账号角色类型

		
		if (layout == null) {
			ctx = this.getActivity();
			parentActivity = (MainActivity) getActivity();
			layout = ctx.getLayoutInflater().inflate(R.layout.fragment_helper,
					null);
			initView();
			initData();
			setOnListener();
			
		} else {
			
			ViewGroup parent = (ViewGroup) layout.getParent();
			if (parent != null) {
				parent.removeView(layout);
			}
		}
		return layout;
	}

	private void setOnListener() {
		// TODO Auto-generated method stub
		ll_userInfo.setOnClickListener(this);
		ll_notify.setOnClickListener(this);
		ll_birthday.setOnClickListener(this);
		ll_createHuodong.setOnClickListener(this);
		ll_appdownload1.setOnClickListener(this);
		ll_comment.setOnClickListener(this);
		ll_leave.setOnClickListener(this);
		ll_tips.setOnClickListener(this);
		ll_appdownload2.setOnClickListener(this);
		ll_setting.setOnClickListener(this);
		tv_exit.setOnClickListener(this);
		ll_qiehuanApp.setOnClickListener(this);
		ll_guanwang.setOnClickListener(this);
		ll_kefu.setOnClickListener(this);
		ll_about.setOnClickListener(this);
		ll_banbenjieshao.setOnClickListener(this);
		ll_tips2.setOnClickListener(this);
		ll_redpacket.setOnClickListener(this);
		ll_redpacket2.setOnClickListener(this);
		ll_jiaoshifengcai.setOnClickListener(this);
		
	}

	private void initView() {
		// TODO Auto-generated method stub
		ll_userInfo = (LinearLayout) layout.findViewById(R.id.ll_userInfo);
		ll_notify = (LinearLayout) layout.findViewById(R.id.ll_notify);
		ll_birthday = (LinearLayout) layout.findViewById(R.id.ll_birthday);
		ll_createHuodong = (LinearLayout) layout.findViewById(R.id.ll_createHuodong);
		ll_appdownload1 = (LinearLayout) layout.findViewById(R.id.ll_appdownload1);
		ll_comment = (LinearLayout) layout.findViewById(R.id.ll_comment);
		ll_leave = (LinearLayout) layout.findViewById(R.id.ll_leave);
		ll_tips = (LinearLayout) layout.findViewById(R.id.ll_tips);
		ll_appdownload2 = (LinearLayout) layout.findViewById(R.id.ll_appdownload2);
		ll_setting = (LinearLayout) layout.findViewById(R.id.ll_setting);
		tv_exit = (TextView) layout.findViewById(R.id.tv_exit);
		ll_bg = (LinearLayout) layout.findViewById(R.id.ll_bg);
		ll_qiehuanApp = (LinearLayout) layout.findViewById(R.id.ll_qiehuanApp);
		ll_guanwang = (LinearLayout) layout.findViewById(R.id.ll_guanwang);
		ll_kefu = (LinearLayout) layout.findViewById(R.id.ll_kefu);
		ll_about = (LinearLayout) layout.findViewById(R.id.ll_about);
		ll_banbenjieshao = (LinearLayout) layout.findViewById(R.id.ll_banbenjieshao);
		ll_tips2 = (LinearLayout) layout.findViewById(R.id.ll_tips2);
		ll_redpacket = (LinearLayout) layout.findViewById(R.id.ll_redpacket);
		ll_redpacket2 = (LinearLayout) layout.findViewById(R.id.ll_redpacket2);
		ll_jiaoshifengcai= (LinearLayout) layout.findViewById(R.id.ll_jiaoshifengcai);
	}

	private void initData() {
		if("2".equals(role)){
			
			ll_bg.setBackgroundResource(R.drawable.teacher1080x639);
			
			ll_notify.setVisibility(ll_notify.GONE);
			ll_comment.setVisibility(ll_comment.GONE);
			ll_leave.setVisibility(ll_leave.GONE);
			ll_birthday.setVisibility(ll_birthday.VISIBLE);
			ll_createHuodong.setVisibility(ll_createHuodong.GONE);
			ll_tips.setVisibility(ll_tips.VISIBLE);
			ll_jiaoshifengcai.setVisibility(ll_tips.VISIBLE);
			ll_redpacket2.setVisibility(ll_tips.VISIBLE);
		}else if("3".equals(role)){
			ll_bg.setBackgroundResource(R.drawable.xiaozhang640x379); 
			Log.e("test", "juese"+role);
			ll_notify.setVisibility(ll_notify.GONE);
			ll_comment.setVisibility(ll_comment.GONE);
			ll_leave.setVisibility(ll_leave.GONE);
			ll_appdownload1.setVisibility(ll_appdownload2.GONE);
			ll_tips.setVisibility(ll_appdownload2.GONE);
			ll_appdownload2.setVisibility(ll_appdownload1.VISIBLE);
			ll_tips2.setVisibility(ll_tips.VISIBLE);
			ll_redpacket.setVisibility(ll_tips.VISIBLE);
			ll_redpacket2.setVisibility(ll_leave.GONE);
			ll_jiaoshifengcai.setVisibility(ll_leave.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_exit: 			//登出
//			SharedPreferences pref = getActivity().getSharedPreferences(
//					SHARE_PREFS_NAME, Context.MODE_PRIVATE);
//			pref.edit().clear().commit();
//			SPUtil.putString(getActivity(), "account", "");
//			SPUtil.putString(getActivity(), "token", "");
//			SPUtil.putString(getActivity(), "token", "");
//			SPUtil.putString(getActivity(), "name", "");
//			SPUtil.putString(getActivity(), "icon", "");
//			SPUtil.putString(getActivity(), "role", "");
//			
//			intent = new Intent(getActivity(),StartActivity.class);
//			startActivity(intent);
//			getActivity().finish();
			
			
			SPUtils.put(getActivity(), ConfigConstant.IS_LOGIN_ON, false);
			SPUtils.put(getActivity(), ConfigConstant.USERTOKEN, "");
			SPUtils.put(getActivity(), ConfigConstant.USERNAME, "");
			SPUtils.put(getActivity(), "Friendlistjson", "");
			SPUtils.put(getActivity(), "haveHomeWork", "0");
			SPUtil.putString(getActivity(), "account", "");
			SPUtil.putString(getActivity(), "token1", "");
			SPUtil.putString(getActivity(), "token2", "");
			SPUtil.putString(getActivity(), "name", "");
			SPUtil.putString(getActivity(), "icon", "");
			SPUtil.putString(getActivity(), "role", "");
			SPUtil.putString(getActivity(), "disableGroup", "");
			
			Intent intent5 = new Intent(getActivity(),StartActivity.class);
			startActivity(intent5);
			RongIM.getInstance().logout();
			getActivity().finish();
			break;
			
		case R.id.ll_userInfo:		// 个人资料
			intent = new Intent(getActivity(),UserInfoActivity.class);
			startActivity(intent);
			
			break;
		case R.id.ll_birthday:		// 生日提醒
			intent = new Intent(getActivity(),BirthdayListActivity.class);
			startActivity(intent);
			
			break;
		case R.id.ll_createHuodong:	// 发布活动
			intent = new Intent(getActivity(),ClassListActivity.class);
			intent.putExtra("view_type", "2");
			startActivity(intent);
			break;
			
		case R.id.ll_appdownload1:	// 应用下载
		case R.id.ll_appdownload2:	
			intent = new Intent(getActivity(),AppdownloadActivity.class);
			startActivity(intent);
			
			break;
		case R.id.ll_comment:		// 评价老师

			intent = new Intent(getActivity(),CommentActivity.class);

			startActivity(intent);
			
			break;
		case R.id.ll_leave:			// 在线请假

			intent = new Intent(getActivity(),LeaveParentListActivity.class);


			startActivity(intent);
			
			break;
		case R.id.ll_tips2:
		case R.id.ll_tips:			// 备忘录
			intent = new Intent(getActivity(),TipsActivity.class);
			startActivity(intent);
			
			break;
		case R.id.ll_redpacket:			// 红包记录（校长端）
			intent = new Intent(getActivity(),RedPackageRecordActivity.class);
			startActivity(intent);
			break;
			
		case R.id.ll_redpacket2:			// 验证红包
			if("1".equals(role)){//家长端
				
				intent = new Intent(getActivity(),MyRedListActivity.class);
				startActivity(intent);
				
			}else if("2".equals(role)){//教师端
				
				intent = new Intent(getActivity(),RedVerifyActivity.class);
				startActivity(intent);
			}
			break;
		case R.id.ll_jiaoshifengcai:			// 教师风采
			intent = new Intent(getActivity(),TeacherShowActivity.class);
			startActivity(intent);
			break;
			
		case R.id.ll_setting:		// 设置
			intent = new Intent(getActivity(),SettingActivity.class);
			startActivity(intent);
			
			break;
		case R.id.ll_qiehuanApp:		// 切换App
			intent = new Intent(getActivity(),ButtonActivity.class);
			startActivity(intent);
			getActivity().finish();
			break; 
		case R.id.ll_guanwang:		// 官网
			intent = new Intent(getActivity(),GuanwangActivity.class);
			startActivity(intent);
			
			break;
		case R.id.ll_kefu:		// 客服
			callPhone();
			break;
		case R.id.ll_about:		// 关于我们
			intent = new Intent(getActivity(),AboutActivity.class);
			startActivity(intent);
			
			break;
		case R.id.ll_banbenjieshao:		// 版本介绍
			intent = new Intent(getActivity(),NewVersionActivity.class);
			startActivity(intent);
			
			break;
			
		default:
			break;
		}
	}

	private void callPhone() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("联系客服");
	  	builder.setMessage("拨打客服电话：18162618663");
	  	builder.setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+"18162618663"));  
                startActivity(intent);  
			}
		});
	  	builder.setNegativeButton("取消", null);
	  	builder.show();
	}
}







